﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace menager_plików
{
    public partial class panel : UserControl
    {
        //pola
        private List<DriveInfo> napedy;
        private string path = "";
        private List<string> pliki;
        private List<string> katalogi;
        //metody i zdarzenia
        public panel()
        {
            InitializeComponent();
        }

        private void panel_Load(object sender, EventArgs e)
        {
            pobierzNapedy();
            comboNapedy.Items.AddRange(napedy.ToArray());
        }
        private void comboNapedy_SelectedIndexChanged(object sender, EventArgs e)
        {
            textAdres.Text = comboNapedy.SelectedItem.ToString();
            path = comboNapedy.SelectedItem.ToString();
            zmienPlikiWLiscie();
        }
        private void textAdres_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) {
                if (Directory.Exists(textAdres.Text)) {
                    path = textAdres.Text;
                    zmienPlikiWLiscie();
                }
                else {
                    textAdres.Text = path;
                }
            }
        }
        //moje metody
        private void pobierzNapedy()
        {
            napedy = new List<DriveInfo>(DriveInfo.GetDrives());
            for (int i = 0; i < napedy.Count; i++)
            {
                if (!napedy[i].IsReady)
                {
                    napedy.Remove(napedy[i]);
                }
            }
        }
        private void zmienPlikiWLiscie() {
            pliki = new List<string>(Directory.GetFiles(path));
            katalogi = new List<string>(Directory.GetDirectories(path));
            listViewPliki.Items.Clear();
            foreach (string folder in katalogi) {
                listViewPliki.Items.Add(folder);
            }
            foreach (string plik in pliki) {
                listViewPliki.Items.Add(plik);
            }
        }  
    }
}
